<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>班型</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="../../../static/css/style.css"  media="all">
    <script type="text/javascript" src="../../../static/js/jquery.min.js" ></script>
    <script src="../../../layui/layui.js" charset="utf-8"></script>
    <script src="../../../static/js/base.js" charset="utf-8"></script>
    <script src="../../../static/js/ax.js"></script>
    <script src="../../../static/js/jquery.cookie.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="../../static/js/html5.js"></script>
    <script src="../../static/js/respond.js"></script>
    <![endif]-->
</head>
<body class="bg_w">
<div class="content-wrap">


    <div class="layui-row">
        <form class="layui-form layui-form-pane" id="from1">
            <input type="hidden" id="dataId" value="" />
            <div class="layui-form-item " id="classCodeItem" style="display: none">
                <label class="layui-form-label">班型编号</label>
                <div class="layui-input-inline">
                    <input type="text" id="classCode" disabled="disabled" value="" class="layui-input" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">班型名称</label>
                <div class="layui-input-inline">
                    <input type="text" id="className" value="" class="layui-input" lay-verify="required" maxlength="100" placeholder="请输入班型名称">
                </div>
                <label class="lheight"><span style="margin-left: 2px;color: red;">*</span>100字符内</label>
            </div>
            <div class="layui-col-space15 layui-form-item">
                <div class="layui-col-sm6">

                    <label class="layui-form-label">班型类别</label>
                    <div class="layui-input-block">
                        <select id="classType">
                            <option value="">请选择</option>
                            <!--<option value="1">一车多人</option>-->
                            <!--<option value="2">一车一人</option>-->
                        </select>
                    </div>

                </div>

                <div class="layui-col-sm6">

                    <label class="layui-form-label">每车人数</label>
                    <div class="layui-input-block">
                        <input type="number" id="peopleNumber" value="" class="layui-input"  placeholder="请输入每车人数">
                    </div>

                </div>

                <div class="layui-col-sm6">

                    <label class="layui-form-label">班级车型</label>
                    <div class="layui-input-block">
                        <select id="carType">
                            <option value="">请选择</option>
                            <!--<option value="1">C1</option>-->
                            <!--<option value="2">C2</option>-->
                            <!--<option value="3">C3</option>-->
                            <!--<option value="4">C4</option>-->
                        </select>
                    </div>

                </div>

                <div class="layui-col-sm6">

                    <label class="layui-form-label">学车时间</label>
                    <div class="layui-input-block">
                        <select id="learnTime">
                            <option value="">请选择</option>
                            <!--<option value="1">周一至周五</option>-->
                            <!--<option value="2">周一至周日</option>-->
                            <!--<option value="3">周末</option>-->
                            <!--<option value="4">夜班</option>-->
                        </select>
                    </div>

                </div>
            </div>


            <div class="layui-form-item layui-form-text mrgintop">
                <label class="layui-form-label">班型描述</label>
                <div class="layui-input-block">
                    <textarea id="classRemark"  placeholder="请输入内容" class="layui-textarea" maxlength="200"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" id="btnClose" class="layui-btn layui-btn-primary btn-left">关闭</button>
                    <button type="button" id="btnSubmit" class="layui-btn add_btn btn-left">提交</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>

<script>

    var pageData = null;
    var baseDataDone = false;
    var $;
    layui.use(['form','layer'], function() {
        $ = layui.$;
        var form = layui.form,
            layer=layui.layer;

        $('#btnClose').on("click", function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        });

        $('#btnSubmit').on('click', function(){
            var classData = { 
                id:$("#dataId").val(),
                name:$("#className").val().trim(),
                type:$("#classType").val(),
                peopleNumber:$("#peopleNumber").val(),
                carId:$("#carType").val(),
                learnId:$("#learnTime").val(),
                reason:$("#classRemark").val()
            };
            if(checkData(classData)) {
                saveData(classData);
            }
        });

        var initBaseData = function () {
            ax.get({
                url: schoolWebUrl + "/api/schoolclass/getBaseData",
                success: function (res) {
                    if (res.code == 0) {
                        loadBaseData(res.data);
                    } else {
                        layer.msg(res.message);
                    }
                }

            });
        };

        var loadBaseData = function (baseData) {
            // 初始化班型
            $("#classType").empty();
            $("#classType").append("<option value=''>请选择</option>");
            $.each(baseData.classTypeList, function (index, classType) {
                $("#classType").append("<option value='" + classType.id + "'>" + classType.code + "</option>");
            });

            // 初始化车型
            $("#carType").empty();
            $("#carType").append("<option value=''>请选择</option>");
            $.each(baseData.carTypeList, function (index, carType) {
                $("#carType").append("<option value='" + carType.id + "'>" + carType.code + "</option>");
            });

            // 初始化学车时间
            $("#learnTime").empty();
            $("#learnTime").append("<option value=''>请选择</option>");
            $.each(baseData.learnTimeList, function (index, learnTime) {
                $("#learnTime").append("<option value='" + learnTime.id + "'>" + learnTime.code + "</option>");
            });
            form.render('select');
            baseDataDone = true;
            loadSelectSelected();
        };

        var initPage = function(dataId) {
            ax.get({
                url: schoolWebUrl + "/api/schoolclass/info",
                data: {id:dataId},
                success: function (res) {
                    if (res.code == 0) {
                        pageData = res.data;
                        loadPage();
                    } else {
                        layer.msg(res.message);
                    }
                }

            });
        };
        var loadPage = function () {
            $("#className").val(pageData.name);
            $("#peopleNumber").val(pageData.peopleNumber);
            $("#classRemark").val(pageData.reason);
            $("#classCode").val(pageData.classNumber);
            $("#classCodeItem").show();
            loadSelectSelected();
        };

        var loadSelectSelected = function () {
            if(baseDataDone && pageData != null) {
                $("#classType").val(pageData.type);
                $("#carType").val(pageData.carId);
                $("#learnTime").val(pageData.learnId);
                form.render('select')
            }
        };
        // 编辑初始化数据
        var dataId = getQueryString("id");
        $("#dataId").val(dataId);
        if(dataId != null && dataId != "") {
            // 有ID，为编辑
            initPage(dataId);
        }
        initBaseData();
    });

    function checkData(classData) {
        var checkResult = true;
        if(classData.name.length == 0) {
            layer.msg("班型名称不能为空");
            checkResult = false;
            if(classData.name.length>100){
                layer.msg("班型名称不能超过100字符");
                checkResult = false;
            }
        } else if(classData.peopleNumber.length == 0) {
            layer.msg("请录入每车人数");
            checkResult = false;
        } else if(parseInt(classData.peopleNumber) <= 0) {
            layer.msg("通知内容不能为空");
            checkResult = false;
        } else if(classData.type.length == "") {
            layer.msg("请选择班型");
            checkResult = false;
        } else if(classData.carId.length == "") {
            layer.msg("请选择车型");
            checkResult = false;
        } else if(classData.learnId.length == "") {
            layer.msg("请选择学车时间");
            checkResult = false;
        }
        return checkResult;
    }
    function saveData(classData) {

        var dataId = $("#dataId").val();
        var index = layer.load(0, {shade: false});
        if(dataId != null && dataId != "") {
            ax.post({
                url: schoolWebUrl + "/api/schoolclass/update",
                data: JSON.stringify(classData),
                success: function (data) {
                    layer.alert(data.message);
                    layer.close(index);
                    if (data.code == 0) {
                        //刷新父页面
                        setTimeout( function(){
                            topFrame.location.reload();
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        }, 2 * 1000 );//延迟2000毫米
                    }
                },
            });
        } else {
            ax.post({
                url: schoolWebUrl + "/api/schoolclass/add",
                data: JSON.stringify(classData),
                success: function (data) {
                    layer.alert(data.message);
                    layer.close(index);
                    if (data.code == 0) {
                        //刷新父页面
                        setTimeout( function(){
                            topFrame.location.reload();
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        }, 2 * 1000 );//延迟2000毫米
                    }
                },
            });
        }
        return false;
    }
</script>
</html>